// temperature.c
// 用于温度监控
#include "freertos/FreeRTOS.h"  // xTaskCreate
#include "driver/temperature_sensor.h"  // temperature_sensor_get_celsius
#include "esp_err.h"  // ESP_ERROR_CHECK
#include "esp_log.h" // ESP_LOGI
#include "common.h"

// 暴露变量
TaskHandle_t temperature_handle = NULL;
// 内部变量
static const char *TAG = "TEMPERATURE";

void temperature_task(void *param) {
  temperature_sensor_handle_t temp_handle = NULL;
  temperature_sensor_config_t temp_config =
    TEMPERATURE_SENSOR_CONFIG_DEFAULT(20, 100);
  ESP_ERROR_CHECK(temperature_sensor_install(&temp_config, &temp_handle));
  ESP_LOGI(TAG, "init finish.");
  while (true) {  // 获取温度数据
    float tsens_out;
    ESP_ERROR_CHECK(temperature_sensor_enable(temp_handle));
    ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
    ESP_ERROR_CHECK(temperature_sensor_disable(temp_handle));
    ESP_LOGI(TAG, "%.1f °C.", tsens_out);
    vTaskDelay(pdMS_TO_TICKS(5000));
  }
}
